package com.ll.menu.user.food.list;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.droid.library.app.emptyview.BaseCallback;
import com.droid.library.app.emptyview.BaseEmptyViewManager;
import com.droid.library.app.emptyview.CallbackAdapter;
import com.droid.library.app.mvp.BaseMVPFragment;
import com.droid.library.app.mvp.list.FooterManager;
import com.droid.library.widget.recyclerview.BaseRecyclerAdapter;
import com.ll.menu.user.R;
import com.ll.menu.user.app.entity.Food;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 食品列表
 *
 * @author wangheng
 */
public class FoodListFragment extends BaseMVPFragment<FoodListPresenter> implements IFoodListUI{

    private static final String KEY_TYPE_ID = "keyTypeId";
    private static final String KEY_TYPE_NAME = "keyTypeName";

    @InjectView(R.id.flFoodListRoot)
    ViewGroup mEmptyViewContainer;

    @InjectView(R.id.tvFoodListType)
    TextView mTypeTextView;

    @InjectView(R.id.rvFoodList)
    RecyclerView mRecyclerView;

    private FoodListEmptyViewManager mEmptyViewManager;
    private FoodListAdapter mAdapter;

    @Override
    protected View onCreateViewExecute(LayoutInflater inflater,
                                       ViewGroup container,
                                       Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_food_list,container,false);

        ButterKnife.inject(FoodListFragment.this,view);

        initView();

        getPresenter().requestList();

        return view;
    }

    private void initView() {
        mEmptyViewManager = new FoodListEmptyViewManager(new CallbackAdapter());
        mEmptyViewManager.addEmptyViewToFirst(getActivity(),mEmptyViewContainer);
        mEmptyViewManager.hideAllEmptyView();

        String typeName = getExtras().getString(KEY_TYPE_NAME);
        mTypeTextView.setText(typeName);

        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3));
        mAdapter = new FoodListAdapter(FoodListFragment.this);
        mRecyclerView.setAdapter(mAdapter);
    }


    @Override
    public BaseRecyclerAdapter<Food> getAdapter() {
        return mAdapter;
    }

    @Override
    public void onPullDownRefreshCompleted() {

    }

    @Override
    public void onPullUpRefreshCompleted() {

    }

    @Override
    public FooterManager getFooterManager() {
        return null;
    }

    @Override
    public BaseEmptyViewManager<? extends BaseCallback> getEmptyViewManager() {
        return mEmptyViewManager;
    }


    @Override
    protected IFoodListUI getUI() {
        return FoodListFragment.this;
    }

    @Override
    protected FoodListPresenter createPresenter() {
        return new FoodListPresenter();
    }

    public static FoodListFragment getInstance(FragmentManager fm,String type, String typeName){
        Fragment savedFragment = fm.findFragmentByTag(type + "_" + typeName);
        FoodListFragment fragment = null;
        if(savedFragment == null || !(savedFragment instanceof FoodListFragment)){
            fragment = new FoodListFragment();
        }else{
            fragment = (FoodListFragment)savedFragment;
        }
        Bundle bundle = new Bundle();
        bundle.putString(KEY_TYPE_ID,type);
        bundle.putString(KEY_TYPE_NAME,typeName);
        fragment.setExtras(bundle);

        return fragment;
    }
}
